﻿
@{
    ViewData["Title"] = "定时任务";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="container-div">
    <div class="row">
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="add();" data-role="system:post:add">
                <i class="fa fa-plus"></i> 新增
            </a>
            <a class="btn btn-primary single disabled" onclick="edit(this);" data-id="0" data-role="system:hangfire:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger single disabled" onclick="removeAll(this);" data-id="0" data-role="system:hangfire:delete">
                <i class="fa fa-remove"></i> 删除
            </a>

            <a class="btn btn-danger single disabled" onclick="pauseJob(this);" data-id="0" data-role="system:hangfire:pause">
                <i class="fa fa-power-off"></i> 停止
            </a>
            <a class="btn btn-success single disabled" onclick="resumeJob(this);" data-id="0" data-role="system:hangfire:resume">
                <i class="fa fa-check"></i> 启动
            </a>
            <a class="btn btn-primary single disabled" onclick="excuteTask(this);" data-id="0" data-role="system:hangfire:excute">
                <i class="fa fa-bolt"></i> 执行
            </a>
            <a class="btn btn-success single disabled" onclick="excuteLog(this);" data-id="0" data-role="system:hangfire:log">
                <i class="fa fa-list"></i> 执行日志
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="dataTable"></table>
        </div>
    </div>
</div>
    @section scripts
{
        <script>
            var $table;
            $(function () {
                loadGrid();
            });

            function loadGrid() {
                var options ={
                    url: '/admin/hangfirejob/getdata',
                    pageNumber: 1,                      // 初始化加载第一页，默认第一页,并记录
                    pageSize: 10,                       // 每页的记录行数（*）
                    pageList: "10, 25, 50, 100",        // 可供选择的每页的行数（*）
                    queryParams: function (params) {
                        let p = {
                            pageNumber: params.pageNumber,
                            pageSize: params.pageSize
                        }
                        return p;
                    },
                    columns: [
                        { field: 'selectItem', radio: true },
                        {
                            field: 'task_name', title: '任务名称', align: 'center', width: 100
                        },
                        {
                            field: 'assembly_namespace', title: '命名空间', align: 'left'
                        },
                        {
                            field: 'class_name', title: '类名', align: 'center'
                        }, {
                            field: 'cron_express', title: 'Cron表达式', align: 'center'
                        }, {
                            field: 'excute_status', title: '任务状态', align: 'center', formatter: function (v) {
                                if (v === 1) {
                                    return '<span class="label label-primary">运行中</span>';
                                }
                                return '<span class="label label-danger">停止</span>';
                            }
                        }, {
                            field: 'excute_time', title: '创建时间', align: 'center'
                        }
                        , {
                            field: 'remark', title: '说明', align: 'center'
                        },
                        {
                            field: 'id', title: '操作', align: 'left', formatter: renderOperationButton
                        },
                    ]

                };

                $table = $('#dataTable').easyTable(options);
                //$table.easyTable('selectedEvent');
            }

            function renderOperationButton(value, row, index) {
                var actions = [];
                actions.push({
                    roleTag: 'system:hangfire:edit',
                    button: '<a class="btn btn-success btn-xs" href="javascript:void(0);" onclick="edit(this);" data-id="' + value + '"><i class="fa fa-edit"></i>编辑</a>'
                });
                actions.push({
                    roleTag: 'system:hangfire:delete',
                    button: '<a class="btn btn-danger btn-xs" href="javascript:void(0);" onclick="removeAll(this);" data-id="' + value + '"><i class="fa fa-trash"></i>删除</a>'
                });

                return jutils.buttonPermission(actions);
            }



            function add() {
                var url = '/admin/hangfirejob/form';
                jutils.dialog('添加任务', url, { id:'' }, null, function () {
                    $table.easyTable("search");
                })
            }

            function edit(obj) {
                var taskId = $(obj).data('id');
                if (jutils.emptyId(taskId)) {
                    var rows = $table.easyTable("selected");
                    if (rows.length == 0) {
                        jutils.warn("请选择数据行");
                        return;
                    }
                    taskId = rows[0].id;
                }
               

                var url = '/admin/hangfirejob/form';
                jutils.dialog('修改任务', url, { id: taskId }, null, function () {
                    $table.easyTable("search");
                })
            }

            function pauseJob(obj) {
                let rows = $table.easyTable('selected');
                if (rows.length == 0) {
                    jutils.warn("请选择数据行");
                    return;
                }
                var id = rows[0].id;
                jutils.confirm('确认停止当前任务吗',
                    function () {
                        jutils.ajaxGet('/admin/hangfireJob/Pause',
                            { id: id },
                            function () {
                                $table.easyTable("search");
                            });
                    });
            }
            function resumeJob(obj) {
                let rows = $table.easyTable('selected');
                if (rows.length == 0) {
                    jutils.warn("请选择数据行");
                    return;
                }
                var id = rows[0].id;
                jutils.confirm('确认启动当前任务吗',
                    function () {
                        jutils.ajaxGet('/admin/hangfireJob/Resume',
                            { id: id },
                            function () {
                                $table.easyTable("search");
                            });
                    });
            }
            function excuteTask(obj) {
                let rows = $table.easyTable('selected');
                if (rows.length == 0) {
                    jutils.warn("请选择数据行");
                    return;
                }
                var id = rows[0].id;
                jutils.confirm('确认执行当前任务吗',
                    function () {
                        jutils.ajaxGet('/admin/hangfireJob/excuteJob',
                            { id: id },
                            function () {
                                $table.easyTable("search");
                            });
                    });
            }

            function removeAll(obj) {
                var id = $(obj).data('id');
                jutils.confirm('确认删除当前任务吗',
                    function () {
                        jutils.ajaxGet('/admin/hangfireJob/DelById',
                            { id: id },
                            function () {
                                $table.easyTable("search");
                            });
                    });
            }
            function excuteLog(obj) {
                let rows = $table.easyTable('selected');
                if (rows.length == 0) {
                    jutils.warn("请选择数据行");
                    return;
                }
                var id = rows[0].id;

                jutils.dialogTop('执行日志', '/admin/hangfirejob/log', { jobId: id }, null, function () {

                });
            }
            function query() {
                $table.easyTable("search");
            }
        </script>
    }






